package com.crowdfunding.util;

import org.apache.http.Header;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.FormBodyPart;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import javax.xml.ws.Response;
import java.io.DataOutputStream;
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpRequestUtils {

    static String Host="http://10.2.48.57:8081/upload/";
    static CloseableHttpClient client= HttpClients.createDefault();
    static RestTemplate restTemplate=new RestTemplate();

    public static String postImageSingle(MultipartFile filein) throws Exception{


//        String URL=Host+"uploadFile";
//        HttpHeaders headers =new HttpHeaders();
//        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
//        MultiValueMap<String,File> file=new LinkedMultiValueMap<>();
//        file.add("file",FileTransferUtils.MultipartFileTransferFile(filein));
//        HttpEntity<MultiValueMap> request=new HttpEntity<>(file,headers);
//        restTemplate.postForEntity(URL,request,String.class);
//        System.out.println(responseEntity);




        String URL=Host+"uploadFile";
//        System.err.println(URL);
//        HttpPost httpPost=new HttpPost(URL);
//        httpPost.setHeader("Content-Type", "multipart/form-data");
//        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//        File file = FileTransferUtils.MultipartFileTransferFile(filein);
//        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
//        builder.addPart("file", new FileBody(file));
//        httpPost.setEntity(builder.build());
//        //执行并拿到响应实体
//        HttpEntity entity = client.execute(httpPost).getEntity();
//        //将实体类的返回结果解析
//        String imageurl=EntityUtils.toString(entity);
//        System.out.println(imageurl);

//        String uploadUrl = Host+"uploadFile";
//        String end = "\r\n";
//        String twoHyphens = "--";
//        String boundary = "******";
//        try {
//            URL url = new URL(uploadUrl);
//            HttpURLConnection httpURLConnection = (HttpURLConnection) url
//                    .openConnection();
//            httpURLConnection.setDoInput(true);
//            httpURLConnection.setDoOutput(true);
//            httpURLConnection.setUseCaches(false);
//            httpURLConnection.setRequestMethod("POST");
//            httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
//            httpURLConnection.setRequestProperty("Charset", "UTF-8");
//            httpURLConnection.setRequestProperty("Content-Type",
//                    "multipart/form-data;boundary=" + boundary);
//            File file = FileTransferUtils.MultipartFileTransferFile(filein);
//            String srcPath = file.getAbsolutePath();
//            DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
//            dos.writeBytes(twoHyphens + boundary + end);
//            dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
//                    + srcPath.substring(srcPath.lastIndexOf("/") + 1)
//                    + "\"" + end);
//            dos.writeBytes(end);
//            System.out.println(url);
//            HttpPost httpPost = new HttpPost(uploadUrl);
//            HttpEntity entity = client.execute(httpPost).getEntity();
//            System.out.println(EntityUtils.toString(entity));
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }

//
//
//        //将MultipartFile转为file类才能进行后续处理
//        final File file = FileTransferUtils.MultipartFileTransferFile(filein);
////        MultiValueMap<String,MultipartFile> param=new LinkedMultiValueMap<>();
////        param.add("file",filein);
//        String result=restTemplate.postForObject(URL,file,String.class);
//        System.err.println(result);


        MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
        bodyMap.add("file", new FileSystemResource(FileTransferUtils.MultipartFileTransferFile(filein)));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(bodyMap, headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.exchange(URL,
                HttpMethod.POST, (org.springframework.http.HttpEntity<?>) requestEntity, String.class);
        System.out.println(response.getBody());
        String back = response.getBody();

        return null;
    }
}
